<?php

namespace ApiBundle\Controller\Admin;

use ApiBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class PersonnelController extends BaseController
{
    /*
     * 编辑/添加人员列表
     */
    public function addPersonnelListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $id = $request->get("id");
        $company_name = $request->get("company_name");
        $name = $request->get("name");
        $phone = $request->get("phone");
        $email = $request->get("email");
        $position = $request->get("position");

        if(empty($company_name) || empty($name) || empty($email) || empty($position)){
            return $this->jsonResponse(1,"参数错误");
        }
        $conn = $this->get("database_connection");
        $strings = $this->get("helper.strings");
        $validations = $this->get("helper.validations");

        if(!$validations->isEmail($email)){
            return $this->jsonResponse(1,"邮箱格式不正确");
        }

        // 添加人员
        if($id == "0"){
            if(empty($phone)){

                return $this->jsonResponse(1,"参数错误");
            }
            if(!$validations->isMobile($phone)){
                return $this->jsonResponse(1,"手机号格式不正确");
            }

            // 判断手机号是否存在
            if($this->checkPhoneExit($phone)){

                return $this->jsonResponse(1,"该手机号已存在");
            }
        }


        // TODO :  公司名称判断
        $is_company = $conn->fetchAssoc("SELECT * FROM company WHERE name = '". $company_name ."'");
        if(!$is_company){
            return $this->jsonResponse(1,"企业不存在");
        }


        $salt = $strings->generateSalt(6,true);
        $password = substr($phone,-6,6);
        $data = array(
            "name" => $name,
            "company_full_name" => $company_name,
            "position_name" => $position,
            "email" => $email,
            "company_id" => $is_company['id']
        );

        if($id == "0"){

            $data['phone'] = $phone;
            $data['salt'] = $salt;
            $data['password'] = $strings->encryptPassword($password,$salt);
            $data['created_at'] = date("Y-m-d H:i:s");
            $data['district_id'] = $this->getDistrictId();

            $conn->insert("user",$data);

        } else if($id > "0"){

            $data['updated_at'] = date("Y-m-d H:i:s");
            $conn->update("user",$data,array("id" => $id));
        }


        return $this->jsonResponse(0,"操作成功");

    }

    /*
     * 禁用/启用帐号
     */
    public function personnelStatusAction(Request $request)
    {
        $status = $request->get("status"); // 0 禁用 1 启用
        $ids = $request->get("ids","[]");
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        if(empty($ids)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");

        $ids_arr = json_decode($ids);
        foreach($ids_arr as $item){
            $conn->update("user",array("status"=>$status),array("id"=>$item));
        }


        if($status == "0"){
            return $this->jsonResponse(0,"禁用成功");
        } else {
            return $this->jsonResponse(0,"启用成功");
        }

    }

    /*
     * 人员管理--人员列表
     */
    public function personalListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $company_name = $request->get("company_name");
        $company_category_id = $request->get("company_category_id");
        $company_attribute_id = $request->get("company_attribute_id");
        $admin_id = $request->get("admin_id");
        $is_technology = $request->get("is_technology"); // 1是 2其他
        $status =$request->get("status"); // 	状态 1启用 2禁用


        $page = $request->get("page","1");

        $rows = $request->get("rows","10");


        $conn = $this->get("database_connection");

        $sql = "SELECT u.*, c.name as company_name, ca.company_attribute_name, cc.company_category_name, a.nickname as admin_name FROM user u LEFT JOIN company c ON u.company_id = c.id  LEFT JOIN company_attribute ca ON c.company_attribute_id = ca.id LEFT JOIN company_category cc ON c.company_category_id = cc.id LEFT JOIN admin a ON c.admin_id = a.id WHERE 1 ";


        $admin_role_id = $this->get("session")->get("admin_role_id");

        if($admin_role_id == 4){
            $sql .= "AND u.company_id IN (SELECT id FROM company WHERE admin_id = '". $this->getAdminId() ."')";
        } else {
            $sql .= "AND u.district_id = '". $this->getDistrictId() ."'";
        }

        // 企业联络人总数
        $all_member_count = count($conn->fetchAll($sql));

        // 科技联络人总数
        $teac_member_count = count($conn->fetchAll($sql . " AND u.technology_liaisons_status = 1 "));

        // 今日新增 企业联络人
        $now_date = date('Y-m-d');
        $new_sql = $sql. " AND DATE_FORMAT(u.created_at,'%Y-%m-%d') = '". $now_date ."' ";


        $new_teac_member_count = count($conn->fetchAll($new_sql));

        if(!empty($company_name)){
            $sql .= " AND c.name = '". $company_name ."'";
        }

        if(!empty($company_category_id)){
            $sql .= " AND c.company_category_id = '". $company_category_id ."'  ";
        }

        if(!empty($company_attribute_id)){
            $sql .= " AND c.company_attribute_id = '". $company_attribute_id ."' ";
        }

        // 科技载体
        if(!empty($admin_id)){
            $sql .= " AND c.admin_id = '". $admin_id ."' ";
        }

        // 科技联络人
        if(!empty($is_technology)){

            if($is_technology == 1){
                $sql .= " AND u.technology_liaisons_status = 1 ";
            } else if($is_technology == 2) {
                $sql .= " AND (u.technology_liaisons_status = -1 OR u.technology_liaisons_status = 0 OR u.technology_liaisons_status = 2) ";
            }

        }

        // 帐号状态
        if(!empty($status)){
            if($status == 1){
                $sql .= " AND u.status = 1 ";
            } else if($status == 2){
                $sql .= " AND u.status = 0 ";
            }

        }

        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";

        // 人员信息
        $data = $conn->fetchAll($sql);

        return $this->jsonResponse(0,"操作成功",array("data" => $data,"all_member_count" => $all_member_count,"teac_member_count" => $teac_member_count,"new_teac_member_count" => $new_teac_member_count,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }

    /*
     * 人员帐号 重置密码
     */
    public function resetPasswordAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $conn = $this->get("database_connection");
        $id = $request->get("id");

        $strings = $this->get("helper.strings");

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $user = $conn->fetchAssoc("SELECT * FROM user WHERE id = '". $id ."' ");

        $password = substr($user['phone'],-6,6);

        $encrypt_password = $strings->encryptPassword($password,$user['salt']);

        $conn->update("user",array("password" => $encrypt_password),array("id" => $id));

        return $this->jsonResponse(0,"操作成功");


    }

}
